﻿using System;
using System.Reflection;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMS.Web.Engine;
using CMS.Web.Data;


namespace CMS.Web.Controllers
{
    [ActionError(ViewName = "~/Hosts/Shared/Error.cshtml", Enabled = true)]
    public class LayoutController : Controller
    {
        private IPagesRepository _pages;     

        public LayoutController(IPagesRepository pages)
        {
            _pages = pages;           
        }

        [HttpPost]
        public ContentResult Index(int id, string zone, string widgets)
        {
            string message = string.Empty;
            try
            {               
                _pages.UpdateWidgetOrders(id, zone, widgets.Split(",").Select(x => x.Convert<int>()).ToList());
                message = "Разметка обновлена.";
            }
            catch (Exception exc)
            {
                message = string.Format("Ошибка при обновлении разметки: {0}.", exc.Message);
            }
            return Content(message);
        }       
    }
}
